<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Home\Controller;
use Think\Controller;

/**
 * 前台首页控制器
 * 主要获取首页聚合数据
 */
class DisneyController extends HomeController {

	//系统首页
    public function index(){

    }
    public function idcardLimit($idcard,$num){
        $map['tour_time'] =array('gt',time());
        $map['identity_card '] =$idcard;
        $map['status'] =array('in',"1,3,5");
        $order =M('order')
                ->join('disneydetail on disneydetail.order_id=`order`.id')
                ->join('contacts on contacts.order_id=`order`.id')
                ->field("contacts.*,disneydetail.*,`order`.*")
                ->where($map)
                //->select();
                ->sum('num');
        if(!$num){
            $num=1;
        }
        $all =$num+$order;
        // $this->response(array('info'=>$map),'json',400);
        if($all>5){
            $this->response(array('info'=>'每个身份证最多沟买5张票'),'json',400);
        }
        return ;
    }
    public function orderAdd(){
        $uid =$this->login();
        if(IS_POST){
            $data = json_decode(file_get_contents('php://input'), true);
            if($data){
                $onedayController =A('Oneday');
                $adult =$data['contacts'];
               
                $myphone =$adult['contact_mobile'];
                $token =$adult['token'];
                $onedayController->checkPhone($myphone);
                $onedayController->checkToken($myphone,$token);
                $identity_card =$adult['identity_card'];
                $onedayController->checkIdCard($identity_card);
                $num =$data['num'];
                $this->idcardLimit($identity_card,$num);
                $json['success'] = true;
                $form_data = $this->orderForm($data);
                if($form_data['success']){

                    $order_info = $form_data['info']; 
                }else{
                    $json['success'] = false;
                    $json['error'] = $form_data['error'];
                }

                if($json['success']){
                    $total_price = $this->calculateTotalPrice($order_info);
                    $order_info['total_price'] = $total_price;
                }  

                if($json['success']){
                    $order_add = $this->addOrder($order_info,$uid);
                    if($order_add['success']){
                        $json['info'] = $order_add['info'];
                        $json['order_id'] = $order_add['order_id'];
                    }else{
                        $json['success'] = false;
                        $json['error'] = $order_add['error'];
                    }
                }

                if($json['success']){
                    $this->ajaxReturn($json);
                }else{
                    $this->ajaxReturn($json,400);
                }
            }

             
        }
    }

    public function disneyHolidayGet(){
        $disneyHolidayModel = D('disneyholiday');
        $disneyholiday_data = $disneyHolidayModel->getDisneyholiday();
        if($disneyholiday_data){
            $json['success'] = true;
            $tmp = $disneyholiday_data;
            $tmp['date_start'] = intval($disneyholiday_data['date_start']);
            $tmp['date_end'] = intval($disneyholiday_data['date_end']);
            $json['info'] = $tmp;
        }else{
            $json['success'] = false;
            $json['error'] = '没有优惠时间';
        }
        $this->ajaxReturn($json);
    }

    public function getDisneyPrice(){
        $disneypriceModel = D('disneyprice');
        $disneyprice_data = $disneypriceModel->getDisneyPrice();
        return $disneyprice_data;
    }

    public function disneyPriceGet(){
        $disneyprice_data =$this->getDisneyPrice();

        foreach ($disneyprice_data as $key => $value) {
            switch ($key) {
                case '1':
                    $save_data['adult_superhot'] = $value;
                    break;
                case '2':
                    $save_data['adult_hot'] = $value;
                    break;
                case '3':
                    $save_data['adult_common'] = $value;
                    break;
                case '4':
                    $save_data['old_child_superhot'] = $value;
                    break;
                case '5':
                    $save_data['old_child_hot'] = $value;
                    break;
                case '6':
                    $save_data['old_child_common'] = $value;
                    break;
                default:
                    # code...
                    break;
                }
                
            };
        
        
        if($disneyprice_data&&count($save_data)==6){
            $json['success'] = true;
            $json['info'] = $save_data;
        }else{
            $json['success'] = false;
            $json['error'] = '获取价格失败';
        }
        $this->ajaxReturn($json);
    }

    protected function getTotalPrice($type=1,$date_start,$date_end){
        $disneyprice = $this->getDisneyPrice();

        if($date_start == $date_end){
            $price = $disneyprice[$this->getPriceType($type,$date_start)];
        }else{
            $price = $disneyprice[$this->getPriceType($type,$date_start)];
            $price += $disneyprice[$this->getPriceType($type,$date_end)];
        }
        //$this->response(array('info'=>$price),'json',400);
        return $price;
    }

    protected function getPriceType($type,$date){
        $weekends = array(0,6);
        //code 超旺季
        $w = date('w',$date);
        $is_holiday = $this->is_Holiday($date);
        $disneyholiday =M('disneyholiday')->find();
        $holiday_begin =$disneyholiday['date_start'];
        $holiday_end =$disneyholiday['date_end'];
        if($is_holiday){
            // 所有节假日 +迪士尼时间段的双休
            $price_type =1;
        }
        //echo date("Y-m-d H:i:s",$date);
        //echo $date;echo $holiday_begin;echo $holiday_end;
        elseif($date>=$holiday_begin&&$date<=$holiday_end&&($w=='0'||$w=='6')){
            //迪士尼时间段6-1 8-31的迪士尼时间段的双休
            //旺季
            $price_type =1;
        }
        elseif($date>=$holiday_begin&&$date<=$holiday_end&&($w!=='0'&&$w!=='6')){
            //迪士尼时间段6-1 8-31的迪士尼时间段的正常
            //旺季
            $price_type =2;
        }
        elseif($w=='0'||$w=='6'){
            //所有双休
            $price_type =2;
        }else{
            //正常
            $price_type =3;
        }
        //$this->response(array('info'=>$price_type),'json',400);
        //是否是节假日
            // $is_holiday = $this->is_Holiday($date);
            // if($is_holiday){
            //     $price_type =1;
            // }else{
            //     //是否是6-16 8-31并且是周末
            //     if((date('m-d',$date)>='06-18')&&(date('m-d',$date)<='08-31')&&(in_array(date('w',$date),$weekends))){
            //          $price_type =1;
            //     }else{
            //         //是否是6-16 8-31或者周末
            //         if(((date('m-d',$date)>='06-18')&&(date('m-d',$date)<='08-31'))||(in_array(date('w',$date),$weekends))){
            //              $price_type =2;
            //         }
            //         //其他
            //         else{
            //              $price_type =3;
            //         }   
            //     }
                
            if($type == 2){
                $price_type+=3;
            }
        return $price_type;
    }

    protected function is_Holiday($date){
        $holidayModel = D('holiday');
        $holiday_data = $holidayModel->getHoliday();
        $is_holiday = 0;
       
        foreach ($holiday_data as $key => $value) {
           
            if($date>=$value['date_start']&&$date<=$value['date_end']){
               
                $is_holiday = 1; 
            }               
           
        }
        return $is_holiday;
    }

    protected function orderForm($order_data){
        /*
        测试数据
        $time = mktime('0','0','0','10','17','2016');
            $data = array(
                'type'  => '6',
                'num'   => '1',
                'order_mark'=>'000',
                'tour_time'=>$time,
                'contacts'=>array(
                    'contact_name'=>'呼啦啦',
                    'identity_card'=>'320623199412170032',
                    'contact_mobile'=>'13775082813',   
                ),
            );
        */
        $json['success'] = true;
        $type_array = array(1,2,3,4,5,6);
        //1成年票2儿童票3老年票4成年人联票5儿童联票6老年联票
        $json = $this->is_set_info($json,$order_data['num'],'num','未设置订票张数',-1);

        $json = $this->is_set_info($json,$order_data['order_remark'],'order_remark','未设置备注',0);

        $json = $this->is_set_info($json,$order_data['tour_time'],'tour_time','未填写时间',-1);

        if($json['success']){
            if(isset($order_data['type'])){
                if(in_array($order_data['type'], $type_array)){
                    $json['info']['type'] = $order_data['type'];
                }else{
                    $json['success'] = false;
                    $json['error'] = '门票种类未选择';
                }
            }else{
                    $json['success'] = false;
                    $json['error'] = '门票种类未选择';
            }
        }

        if($json['success']){
            if(isset($order_data['contacts'])){
                $contacts = $order_data['contacts'];
                if(count($contacts) == 0){
                    $json['success'] = false;
                    $json['error'] = '没有游客信息';
                }else{
                   
                        if($contacts['contact_name'] == null && $json['success']){
                            $json['success'] = false;
                            $json['error'] = '姓名未填写';
                        }
                        if($contacts['identity_card'] == null ){
                            $json['success'] = false;
                            $json['error'] = '未填写身份证';
                        }else{
                            // if(!preg_match('/^([\d]{17}[xX\d]$/',$value['identity_card'])){
                            //     $json['success'] = false;
                            //     $json['error'] = '身份证号不对';
                            // }
                        }
                        if($contacts['contact_mobile'] == null){
                            $json['success'] = false;
                            $json['error'] = '手机号未填写';
                        }else{
                            // if(!preg_match('/^1([3][0-9]|[4][57]|[5][0-3|5-9]|[8][0-9]|[7][0])[\d]{8}$/',$value['contact_mobile'])){
                            //     $json['success'] = false;
                            //     $json['error'] = '手机号不对';
                            // } 
                        }
                        $contacts['contact_type'] = $order_data['type'];
                    
                }
                

            }else{
                    $json['success'] = false;
                    $json['error'] = '没有游客信息';
            }
        } 

        if($json['success']){
            $json['info']['contacts']['0'] = $contacts;
        }

        if($json['success']){
            $json['info']['project'] = 2;
        }

        return $json;
        
    }
    //老人和小孩0.75，联票0.95
    protected function calculateTotalPrice($order_info){
        
       
        switch ($order_info['type']) {
            case '1':
            //成人
                $price = $order_info['num']*$this->getTotalPrice(1,$order_info['tour_time'],$order_info['tour_time']);
                break;
            case '2':
            //儿童
                $price = $order_info['num']*$this->getTotalPrice(2,$order_info['tour_time'],$order_info['tour_time']);
                break;
            case '3':
            //老年
                $price = $order_info['num']*$this->getTotalPrice(2,$order_info['tour_time'],$order_info['tour_time']);
                break;
            case '4':
            //成人联票
                $price = 0.95*$order_info['num']*$this->getTotalPrice(1,$order_info['tour_time'],$order_info['tour_time']+86400);
                break;
            case '5':
            //儿童联票
                $price = 0.95*$order_info['num']*$this->getTotalPrice(2,$order_info['tour_time'],$order_info['tour_time']+86400);
                break;
            case '6':
            //老年联票
                $price = 0.95*$order_info['num']*$this->getTotalPrice(2,$order_info['tour_time'],$order_info['tour_time']+86400);
                break;
            default:
                break;
        }
       
        return $price;
    }

    protected function addOrder($order_info,$uid){

        $json['success'] = true;

        $order['is_cancel'] = 1;

        if($json['success']){ 

            if(!$uid){
               
                $json['success'] = false;
                $json['error'] = '尚未登录';
            }
        }
        $order['uid'] = $uid;

        $order['date_added'] = time();

        $order['order_type'] = 1;

        $order['status'] = 1;

        $order['project'] = $order_info['project'];

        $order['order_price'] = $order_info['total_price'];

        $order['order_remark'] = $order_info['order_remark'];

        $info['tour_time'] = $order_info['tour_time'];

        $info['num'] = $order_info['num'];

        $contacts = $order_info['contacts'];
        if($json['success']){
            $orderModel = D('order');
            $order_id = $orderModel->addOrder($order,$uid);
            if(!$order_id){
               
                $json['success'] = false;
                $json['error'] = '添加订单出错';
            }
        }
        
        if($json['success']){
            $info['order_id'] = $order_id;
            $disneyModel = D('disneydetail');
            $disney_data = $disneyModel->addDisneyDetail($info);
            if(!$disney_data){
                $json['success'] = false;
                $json['error'] = '添加订单详情出错';
            }
        }

        if($json['success']){
            $info['order_id'] = $order_id;
            $contactsModel = D('contacts');
            $contact_data = $contactsModel->addContacts($contacts,$order_id);
            if(!$contact_data){
                $json['success'] = false;
                $json['error'] = '添加联系人出错';
            }
        }
        $json['order_id'] = $order_id;

        if($json['success']){
            $json['info'] = '添加成功';
        }
        return $json;
    }

}